// pages/volunteer/volunteer.js
const app = getApp()
Page({
  data: {
    requestUrl: app.globalData.requestUrl,
    nations: [
      "汉族", "壮族", "满族", "回族", "苗族", "维吾尔族", "土家族", "彝族", "蒙古族", "藏族",
      "布依族", "侗族", "瑶族", "朝鲜族", "白族", "哈尼族", "哈萨克族", "黎族", "傣族", "畲族",
      "傈僳族", "仡佬族", "东乡族", "高山族", "拉祜族", "水族", "佤族", "纳西族", "羌族", "土族",
      "仫佬族", "锡伯族", "柯尔克孜族", "达斡尔族", "景颇族", "毛南族", "撒拉族", "布朗族", "塔吉克族",
      "阿昌族", "普米族", "鄂温克族", "怒族", "京族", "基诺族", "德昂族", "保安族", "俄罗斯族", 
      "裕固族", "乌孜别克族", "门巴族", "鄂伦春族", "独龙族", "塔塔尔族", "赫哲族", "珞巴族", "基诺族"
    ],
    educations: ['本科', '硕士', '博士', '其他'],

    name:'',//姓名
    gender:'',//性别
    nationIndex:0,//民族下标选项
    eduLevelIndex:0,//学历下标选项
    resume:'',//个人简历
    family:'',//家庭成员
    socialRelations:'',//主要社会关系
    partyKnowledge:'',//对党的认识
    volunteerContent:'',//入党志愿
    handwrittenDoc:'',//手写志愿书图片
    isAgree:'',//是否选择承诺真实有效

  },
  nameInput(e){
    this.setData({
      name:e.detail.value
    });
  },
  onGenderChange(e){
    this.setData({
      gender:e.detail.value
    });
  },
  nationChange(e){
    this.setData({
      nationIndex:e.detail.value
    });
  },
  educationChange(e){
    this.setData({
      eduLevelIndex:e.detail.value
    });
  },
  resumeInput(e){
    this.setData({
      resume:e.detail.value
    });
  },
  familyInput(e){
    this.setData({
      family:e.detail.value
    });
  },
  socialRelationsInput(e){
    this.setData({
      socialRelations:e.detail.value
    });
  },
  partyKnowledgeInput(e){
    this.setData({
      partyKnowledge:e.detail.value
    });
  },
  volunteerContentInput(e){
    this.setData({
      volunteerContent:e.detail.value
    });
  },
  agreeChange(e){
    if(e.detail.value.length>0){
      this.setData({
        isAgree:'agree'
      });
    }else{
      this.setData({
        isAgree:''
      });
    }
  },
  chooseImage() {
    let that=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        wx.uploadFile({
          url: app.globalData.requestUrl + '/user/uploadImage', //接受图片的接口地址
          filePath: res.tempFilePaths[0],
          name: 'file',
          success(res1) {
            let returnData = JSON.parse(res1.data);
            let imgUrl =app.globalData.requestUrl+ returnData.imgUrl;
            that.setData({
              handwrittenDoc: imgUrl
            })
          }
        })
       
      }
    })
  },

  //点击提交
  formSubmit(e) {
    let that = this;
    if(that.data.name==''){
      wx.showToast({ title: '请填写姓名', icon: 'none' })
      return;
    }
    if(that.data.gender==''){
      wx.showToast({ title: '请选择性别', icon: 'none' })
      return;
    }
    if(that.data.resume==''){
      wx.showToast({ title: '请填写个人简历', icon: 'none' })
      return;
    }
    if(that.data.family==''){
      wx.showToast({ title: '请填写家庭成员', icon: 'none' })
      return;
    }
    if(that.data.socialRelations==''){
      wx.showToast({ title: '请填写主要社会关系', icon: 'none' })
      return;
    }
    if(that.data.partyKnowledge==''){
      wx.showToast({ title: '请填写对党的认识', icon: 'none' })
      return;
    }
    if(that.data.volunteerContent==''){
      wx.showToast({ title: '请填写入党志愿', icon: 'none' })
      return;
    }
    if(that.data.handwrittenDoc==''){
      wx.showToast({ title: '请上传手写图片', icon: 'none' })
      return;
    }
    if(that.data.isAgree==''){
      wx.showToast({ title: '请勾选承诺选择框', icon: 'none' })
      return;
    }

    let param = {
      name:that.data.name,
      gender:that.data.gender,
      nation:that.data.nations[that.data.nationIndex],
      eduLevel:that.data.educations[that.data.eduLevelIndex],
      resume:that.data.resume,
      family:that.data.family,
      socialRelations:that.data.socialRelations,
      partyKnowledge:that.data.partyKnowledge,
      volunteerContent:that.data.volunteerContent,
      handwrittenDoc:that.data.handwrittenDoc,
      userId:wx.getStorageSync('user').id
    };
    wx.showLoading({
      title: '提交中...',
    })
    


    // 实际应在此处处理图片和表单数据的上传
    setTimeout(() => {
      wx.hideLoading();
      wx.request({
        url: app.globalData.requestUrl + '/party/volunteer/submit',
        method: 'post',
        data: param,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          if(res.data.returnCode==200){
            wx.showToast({
              title: '提交成功',
              icon: 'success'
            })
          }else if(res.data.returnCode==400){
            wx.showToast({
              title: '请勿重复提交',
              icon: 'error'
            })
          }else if(res.data.returnCode==100){
            wx.showToast({ title: '成为预备党员方可提交入党志愿', icon: 'none' })
          }
        }
      });
    }, 1500)
  },


})